package com.vshop.platform.system.crud.service;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.vshop.framework.core.util.MapstructUtil;
import com.vshop.framework.mybatisflex.page.PageData;
import com.vshop.framework.mybatisflex.page.PageQuery;
import com.vshop.platform.system.crud.dao.SysLoginLogDao;
import com.vshop.platform.system.domain.entity.SysLoginLog;
import com.vshop.platform.system.domain.param.SysLoginLogParam;
import com.vshop.platform.system.domain.vo.SysLoginLogVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class SysLoginLogService {

    private final SysLoginLogDao sysLoginLogDao;

    public PageData<SysLoginLogVo> page(SysLoginLogParam param, PageQuery pageQuery) {
        QueryWrapper queryWrapper = sysLoginLogDao.buildQueryWrapper(param, pageQuery);
        Page<SysLoginLogVo> page = sysLoginLogDao.pageAs(pageQuery.build(), queryWrapper, SysLoginLogVo.class);
        return PageData.build(page);
    }

    public boolean save(SysLoginLogParam param) {
        SysLoginLog entity = MapstructUtil.convert(param, SysLoginLog.class);
        entity.setLoginTime(LocalDateTimeUtil.now());
        return sysLoginLogDao.save(entity);
    }

}
